package edu.ucla.cs.cs144;

import java.io.*;
import java.text.*;
import java.util.*;

import java.io.IOException;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;

import edu.ucla.cs.cs144.Tag;
import edu.ucla.cs.cs144.PayNow;

public class CreditCardServlet extends HttpServlet implements Servlet {
       
    public CreditCardServlet() {}
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    	HttpSession session = request.getSession(true);		// Don't know what to do with this yet
        
        PayNow paynow = (PayNow)session.getAttribute("paynow");
        
    	paynow.CreditCard = request.getParameter("cc");
		session.setAttribute("paynow", paynow);
		
		JSONObject json = new JSONObject();
		
		json.put("ItemID", paynow.ItemID);
		json.put("ItemName", paynow.ItemName);
		json.put("BuyPrice", paynow.BuyPrice);
		json.put("CreditCard", paynow.CreditCard);
			
		request.setAttribute("result", json.toString());
		request.getRequestDispatcher("/creditCardResult.jsp").forward(request, response);
    }
}
